//
//  TKYTabbar.m
//  常用的类和基类
//
//  Created by apple on 2018/3/16.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYTabbar.h"
#import "UIView+Extension.h"
@interface TKYTabbar ()

/** 中间的发布按钮 */
@property (nonatomic, strong) UIButton *publishButton;

@end
@implementation TKYTabbar

#pragma mark - 懒加载
/** 发布按钮 */
- (UIButton *)publishButton
{
    if (!_publishButton) {
        UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
       // [publishButton setTitle:@"点击" forState:UIControlStateNormal];
       // [publishButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //[publishButton setTitle:@"发布" forState:UIControlStateHighlighted];

        [publishButton setImage:[UIImage imageNamed:@"add_btn"] forState:UIControlStateNormal];
        [publishButton setImage:[UIImage imageNamed:@"add_btn"] forState:UIControlStateHighlighted];
        [publishButton addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:publishButton];
        _publishButton = publishButton;
    }
    return _publishButton;
}
#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        //设置tabbar 透明背景
        //self.backgroundImage = [self imageWithColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:0]];
       // self.shadowImage = [UIImage new];
        //self.backgroundImage = [UIImage imageNamed:@"tabbar-light"];
    }
    return self;
}

- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f,0.0f, 1.0f,1.0f);
    
    UIGraphicsBeginImageContext(rect.size);
    
    CGContextRef context =UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    
    CGContextFillRect(context, rect);
    
    UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
}

/**
 *  布局子控件
 */
- (void)layoutSubviews
{
    [super layoutSubviews];
    /**** 按钮的尺寸 ****/
    CGFloat buttonW = self.kx_width / 5;
    CGFloat buttonH = self.kx_height;
    
    /**** 设置所有UITabBarButton的frame ****/
    CGFloat tabBarButtonY = 0;
    // 按钮索引
    int tabBarButtonIndex = 0;
    
    for (UIView *subview in self.subviews) {
        // 过滤掉非UITabBarButton
        if (subview.class != NSClassFromString(@"UITabBarButton")) continue;
        
        // 设置frame
        CGFloat tabBarButtonX = tabBarButtonIndex * buttonW;
        if (tabBarButtonIndex >= 2) { // 右边的2个UITabBarButton
            tabBarButtonX += buttonW;
        }
        subview.frame = CGRectMake(tabBarButtonX, tabBarButtonY, buttonW, 50);
        
        // 增加索引
        tabBarButtonIndex++;
    }
    
    /**** 设置中间的发布按钮的frame ****/
    self.publishButton.kx_width = buttonW;
    self.publishButton.kx_height = buttonH;
    self.publishButton.kx_centerX = self.kx_width * 0.5;
    if (IS_iPhoneX) {
         self.publishButton.kx_centerY = self.kx_height * 0.5 -10;
    }else{
         self.publishButton.kx_centerY = self.kx_height * 0.5;
    }
   
}

#pragma mark - 监听
- (void)publishClick
{
    NSLog(@"点击中间的按钮");
    if([self.TKYTabBarDelegate respondsToSelector:@selector(addButtonClick:)]){
        [self.TKYTabBarDelegate addButtonClick:self];
        }
}


@end
